import json

from flask import render_template,request,jsonify
from . import servers

import os
from frameworks.cores.Config import *
from frameworks.utils.JsonResult2 import *
from frameworks.cores.LoadClass import *

basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
config = Config(basepath)

apimap = config.getApi()
env = apimap["base"]
print(env)
from flask_session import Session

jsonResult = JsonResult2()

loader = LoadClass()

@servers.route('/server/list')
def server_list():
    return render_template('servers/server_list.html')

@servers.route(env+"/server/update",methods=['POST'])
def update():
    response = request.get_data(as_text=True)
    rs = loader.load("controller.servers.ServerController", "ServerController", {
        "func": "update",
        "data": json.loads(response)
    })
    return jsonResult.setOk([], "更新成功")

@servers.route(env+"/server/delete",methods=['GET'])
def delete():
    id = request.args.get("id",0)
    print(id)
    rs = loader.load("controller.servers.ServerController", "ServerController", {
        "func": "delete",
        "data": {"id":id}
    })
    return jsonResult.setOk([], "删除成功")

@servers.route(env+"/server/add",methods=['POST'])
def add():
    response = request.get_data(as_text=True)
    rs = loader.load("controller.servers.ServerController", "ServerController", {
        "func": "add",
        "data": json.loads(response)
    })
    return jsonResult.setOk([], "添加成功")

@servers.route(env+"/server/form",methods=['GET', 'POST'])
def form():
    model = request.args.get("model", "school")
    id = request.args.get("id", "0")
    obj = loader.load("controller.servers.ServerController","ServerController",{
        "func":"getRowData",
        "fields":["id","server_name","area_name","ip","ssh_user","ssh_pass","ssh_port","is_deploy_cluster"],
        "id": id
    })
    fields = loader.load("controller.servers.ServerController","ServerController",{
        "func":"getMap",
        "fields":["id","server_name","area_name","ip"]
    })
    return render_template(
        'servers/server_form.html',
        obj=obj[0],
        fields=fields
    )

@servers.route(env+"/server/formadd",methods=['GET', 'POST'])
def formadd():
    model = request.args.get("model", "school")
    fields = loader.load("controller.servers.ServerController","ServerController",{
        "func":"getMap",
        "fields":["id","server_name","ip","ssh_user","ssh_pass","ssh_port","is_deploy_cluster"]
    })
    return render_template(
        'servers/server_formadd.html',
        fields=fields
    )

@servers.route(env+"/server/cols",methods=['GET','POST'])
def cols():
    rs = loader.load("controller.servers.ServerController", "ServerController", {
        "func":"getColumns",
        "fields":["id","server_name","area_name","ip","ssh_user","ssh_pass","ssh_port","is_deploy_cluster","ctime","mtime"]
    })
    return jsonResult.setOk(rs,"获取成功")

@servers.route(env+"/server/page",methods=['GET'])
def user():
    num = request.args.get("limit",10)
    page = request.args.get("page",1)
    areaId = request.args.get("area_id",0)
    if areaId == 0:
        data = loader.load("controller.servers.ServerController", "ServerController", {
            "func": "getData",
            "fields": ["id","server_name","area_name","ip","ssh_user","ssh_pass","ssh_port","is_deploy_cluster","ctime","mtime"],
            "page": page,
            "num": num,
            "orderfield": "id",
            "asc": "asc",
            "where": "1"
        })
        return json.dumps(data)
    else:
        wherestr = "area_id=" + str(areaId)
        data = loader.load("controller.servers.ServerController", "ServerController", {
            "func": "getData",
            "fields": ["id", "server_name", "area_name", "ip","ssh_user","ssh_pass","ssh_port","is_deploy_cluster", "ctime", "mtime"],
            "page": page,
            "num": num,
            "orderfield": "id",
            "asc": "asc",
            "where": wherestr
        })
        return json.dumps(data)